<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        //获取当前守卫的值
        $guards = $exception->guards();
        //通过判断$guards的值里面是否有admin的值,如果有,代表想跳转去后台登录,如果没有,就代表跳转去前台登录
        if (in_array('admin',$guards)){
            $url = '/admin/login';
        }else{
            $url = '/login';
        }
        if ($request->expectsJson()){
            //如果是异步请求,进这个if
            return response()->json(['message' => $exception->getMessage()], 401);
        }else{
            //如果不是异步请求,就进入else
            return redirect()->guest($url);
        }
    }


    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }


}
